package pl.edu.pw.stud.ltrzaska.test;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.log4j.Logger;
import org.junit.Test;

import pl.edu.pw.stud.ltrzaska.exception.FilesysPolicyException;
import pl.edu.pw.stud.ltrzaska.filesystem.VirtualFsReader;
import pl.edu.pw.stud.ltrzaska.logic.FsEquivalentsFinder;
import pl.edu.pw.stud.ltrzaska.logic.FsObjectEquivalent;
import pl.edu.pw.stud.ltrzaska.logic.PolicyLoader;
import pl.edu.pw.stud.ltrzaska.logic.ValidationContext;
import pl.edu.pw.stud.ltrzaska.policy.entity.Directory;
import pl.edu.pw.stud.ltrzaska.policy.entity.FilesystemObject;
import pl.edu.pw.stud.ltrzaska.policy.entity.Person;
import pl.edu.pw.stud.ltrzaska.policy.entity.Policy;

public class LoadPolicyTestCase {
	private static final Logger LOGGER = Logger.getLogger(LoadPolicyTestCase.class);

	@Test
	public void loadCorrectPolicy() throws FilesysPolicyException {
		LOGGER.debug("Running test " + LoadPolicyTestCase.class.getName());
		String curDir = System.getProperty("user.dir");

		File policyFile = new File(curDir, "/tests/test-filesystem/policy-1.xml");
		
		PolicyLoader pl = new PolicyLoader(policyFile);
		Policy policy = pl.getPolicy();

		assertTrue(policy != null);
		assertTrue(policy.getFilesystem() != null);
		assertTrue(policy.getFilesystem().getChildren() != null);
		assertTrue(policy.getFilesystem().getChildren().size() > 0);

		ArrayList<FilesystemObject> objects = policy.getFilesystem().getChildren();
		assertTrue("plik opisu projektu".equals(objects.get(0).getRefname()));
		assertTrue("10KB".equals((String) objects.get(2).getMinSize()));
		
		// sprawdzenie dodatkowego atrybutu
		assertTrue("opis projektu".equals((String) objects.get(0).getAdditionalAttribtue("content-pattern")));

		// sprawdzenie odwolan obiektow do siebie
		Person janb = ((Person) pl.getObject("janb"));
		assertTrue("Jan Brzechwa".equals(janb.getName()));
		Directory dir = (Directory) pl.getObject("aut");
		assertTrue(dir.getChildren().size() == 2);
		assertTrue("wspomagajacy".equals(dir.getChildren().get(1).getName()));
		
		assertTrue("10KB".equals((String) objects.get(2).getMinSize()));
		// sprawdzenie dziedziczenia
		assertTrue("12KB".equals((String) objects.get(1).getMinSize()));
	}

}
